<template>
  <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="地区">
        <el-input
          v-model="searchForm.region"
          placeholder="地区"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 18.75rem">
        <el-date-picker
          v-model="betweenTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-table
    :data="PositionList"
    border
    style="width: 90%; margin: 20px auto;border-radius: 8px;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :cell-style="{ textAlign: 'center' }"
  >
    <el-table-column prop="region" label="地区" width="180"> </el-table-column>
    <el-table-column prop="coefficient" label="地区系数" width="180" />
    <el-table-column
      prop="state"
      label="状态"
      width="82"
      align="center"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.state === 0 ?'danger' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未启用" : "启用" }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column prop="ins" label="备注" />
    <el-table-column prop="updateTime" label="最后更新时间" width="200" />
  </el-table>
  <pagination
    class="pagination"
    v-show="PositionPage.total > 0"
    :total="PositionPage.total"
    v-model:page="searchForm.page"
    v-model:limit="PositionPage.size"
    @pagination="handlePagination"
  />
</template>
<script setup>
import {ref,onMounted,onBeforeMount,reactive,defineEmits,watch,} from "vue";
import { getRegionList } from "@/api/api";
import {getRegionPastPage,getRegionPastSearch} from "@/api/regionCoefficient";
import { ElMessage } from "element-plus";

const betweenTime=ref(null)
const searchForm = ref({
  region:"",
  page: 1,
  page_size: 10,
  beginTime: "",
  endTime: "",
});
const PositionPage=ref([])
const PositionList=ref([])
onBeforeMount(() => {
  getRegionPastPage(searchForm.value).then((data) => {
    PositionPage.value=data.data;
    PositionList.value=data.data.records;
  });
});

watch(
  PositionList,
  (newVal) => {
    console.log("list发生了改变");
  },
  { deep: true }
);



const searchEvent=()=>{
  if(betweenTime.value!=null){
    searchForm.value.beginTime=betweenTime.value[0]
    searchForm.value.endTime=betweenTime.value[1]
  }
  getRegionPastSearch(searchForm.value).then(data=>{
      PositionPage.value=data.data;
    PositionList.value=data.data.records;
    })
}

const handlePagination=(value)=>{
  if(searchForm.value.region==""&&searchForm.value.betweenTime==""){
    getRegionPastPage(value).then(data=>{
        PositionPage.value=data.data;
        PositionList.value=data.data.records;
    })
  }
  else{
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getRegionPastSearch(searchForm.value).then(data=>{
      PositionPage.value=data.data;
    PositionList.value=data.data.records;
    })
  } 
}

const update = () => {
  searchForm.value.region=""
  searchForm.value.beginTime=""
  searchForm.value.endTime=""
  searchForm.value.betweenTime=""
  getRegionPastPage(searchForm.value).then((data) => {
    PositionPage.value=data.data;
    PositionList.value=data.data.records;
  });
};
</script>

<style lang="scss" scoped>
.title1 {
  color: #95d475;
  width: 200px;
  height: 35px;
  font-size: 25px;
  background: rgba(179, 225, 157, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 20px auto;
  line-height: 35px;
}
</style>